#include<bits/stdc++.h>
using namespace std;
int T,n,m;
int x[8011],y[8011],o[8011];
int D(int i,int j)
{
	return abs(x[i] - x[j]) + abs(y[i] - y[j]);
}
bool bl[100011];
const int k = 100000; 
int a,b,c;
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	freopen("chess.in","r",stdin);
	freopen("chess.out","w",stdout);
	bl[1] = 1;
	for(int i = 2;i <= k;i++)
	{
		if(!bl[i])
			for(int j = 2 * i;j <= k;j += i)
				bl[j] = 1;
	}
	cin >> T;
	while(T--)
	{
		cin >> n >> m;
		for(int i = 1;i <= n;i++)
			cin >> x[i] >> y[i];
		for(int i = 1;i <= n;i++)
			cin >> o[i];
		long long ans = 0;
		for(int i = 1;i <= n;i++)
			for(int j = i + 1;j <= n;j++)
				for(int k = j + 1;k <= n;k++)
				{
					a = D(i,j),b = D(i,k),c = D(j,k),m = ((a ^ b ^ c) ^ max({a,b,c}) ^ min({a,b,c}));
					if(!bl[m] && (m % 20 == 3 || m <= 10))
						ans += o[i] + o[j] + o[k];
				}
		cout << ans << "\n";
	}
	return 0;
}
